package com.jdbc;

import cn.hutool.http.HttpUtil;
import com.JsonGetter.Getter;
import com.JsonUtils.PhoneInfo.Info;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * @author Lenovo
 */

public class ConnUtil implements AutoCloseable{
    /**
     * 登陆基本配置信息
     */
    private String sqlUrl;
    private String sqlType;
    private String admin;
    private String password;
    //数据库连接对象
    private Connection conn = null;
    //数据库操作对象
    private Statement sta = null;
    //数据库查询结果对象
    private ResultSet rs = null;


    public String getSqlUrl() {
        return sqlUrl;
    }

    public void setSqlUrl(String sqlUrl) {
        this.sqlUrl = sqlUrl;
    }

    public String getSqlType() {
        return sqlType;
    }

    public void setSqlType(String sqlType) {
        this.sqlType = sqlType;
    }

    public ConnUtil() {
        //获取来自文本的配置信息
        String doc = "src/main/java/com/jdbc/jdbcConnect.properties";
        Properties p = new Properties();
        try {
            p.load(new FileInputStream(doc));
            sqlType = p.getProperty("sqlType");
            sqlUrl = p.getProperty("sqlUrl");
            admin = p.getProperty("admin");
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println(e + "文件加载" + doc + "error!");
        }
    }

    public ConnUtil(String sqlUrl, String sqlType, String admin, String password) {
        this.sqlUrl = sqlUrl;
        this.sqlType = sqlType;
        this.admin = admin;
        this.password = password;
    }
    public void getConnect(){
        //生成连接
        try{
            //获取驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            //创建连接
            conn = DriverManager.getConnection(sqlUrl,admin,password);
            //获取操作对象
            sta = conn.createStatement();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    public String select(String sql) {
        try{

            //处理查询结果集
            rs = sta.executeQuery(sql);
            if(rs.next()){
                return "基本信息:\n开卡地:" + rs.getString("local") +
                        "\n卡类型:"+ rs.getString("type") + "\n使用:" +rs.getString("bz");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public boolean Insert(String sql){
        try {
            sta.execute(sql);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    @Override
    public void close(){
        //释放资源
        try {
            if(rs != null){
                rs.close();
            }

            if(sta != null){
                sta.close();
            }

            if (conn != null) {
                conn.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try(ConnUtil connUtil = new ConnUtil()){
            connUtil.getConnect();
            String phoneNumber = "13145211010";
            String s = connUtil.select("select local,type,bz from phone where phone = '" + phoneNumber + "'");
            if(s == null){
                String get = HttpUtil.get("https://api.vvhan.com/api/phone?tel=" + phoneNumber);
                var format = Getter.getPhone(get);
                System.out.println(format.getAll());
                Info info = format.getInfo();
                connUtil.Insert("insert into phone(phone,local,type,bz) values('" + phoneNumber+ "','" + info.getLocal() + "','" + info.getType() + "','" +
                        info.getBz() + "')");
            }else{
                System.out.println("电话号码:" + phoneNumber + "\n" + s);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
